package com.wedo.mall.activity.user;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.e.library.activity.EActivity;
import com.e.library.entity.User;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.ESPUtils;
import com.wedo.mall.R;
import com.wedo.mall.activity.login.WLogin;
import com.wedo.mall.dialog.PayPasswordDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.wedo.mall.activity.login.WLogin.USER;

public class MyWalletActivity extends EActivity {

    @BindView(R.id.iv_mywallet_bg) ImageView iv_mywallet_bg;
    @BindView(R.id.tv_money) TextView tv_money;
    @BindView(R.id.tv_rise) TextView tv_rise;
    @BindView(R.id.label_code) TextView label_code;

    protected User user;
    protected boolean hasPassword;
    protected boolean firstInput;
    protected String tmpPassWord;
    // 对话框
    private View my_dialog_View;
    private Dialog my_dialog;
    private LinearLayout ll_dismiss;
    private TextView tv_pw1,tv_pw2,tv_pw3,tv_pw4,tv_pw5,tv_pw6,
            tv_number1,tv_number2,tv_number3,tv_number4,tv_number5,tv_number6,tv_number7,tv_number8,tv_number9,tv_number0,tv_title;
    private ImageView iv_delete;

    @Override
    protected int layout() {
        return R.layout.activity_my_wallet;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        fixMyWalletBGHeight();


        // 对话框
        my_dialog_View = LayoutInflater.from(this).inflate(
                R.layout.dialog_pay_password, null);

        tv_pw1 = (TextView) my_dialog_View.findViewById(R.id.tv_pw1);
        tv_pw2 = (TextView) my_dialog_View.findViewById(R.id.tv_pw2);
        tv_pw3 = (TextView) my_dialog_View.findViewById(R.id.tv_pw3);
        tv_pw4 = (TextView) my_dialog_View.findViewById(R.id.tv_pw4);
        tv_pw5 = (TextView) my_dialog_View.findViewById(R.id.tv_pw5);
        tv_pw6 = (TextView) my_dialog_View.findViewById(R.id.tv_pw6);
        tv_number1 = (TextView) my_dialog_View.findViewById(R.id.tv_number1);
        tv_number2 = (TextView) my_dialog_View.findViewById(R.id.tv_number2);
        tv_number3 = (TextView) my_dialog_View.findViewById(R.id.tv_number3);
        tv_number4 = (TextView) my_dialog_View.findViewById(R.id.tv_number4);
        tv_number5 = (TextView) my_dialog_View.findViewById(R.id.tv_number5);
        tv_number6 = (TextView) my_dialog_View.findViewById(R.id.tv_number6);
        tv_number7 = (TextView) my_dialog_View.findViewById(R.id.tv_number7);
        tv_number8 = (TextView) my_dialog_View.findViewById(R.id.tv_number8);
        tv_number9 = (TextView) my_dialog_View.findViewById(R.id.tv_number9);
        tv_number0 = (TextView) my_dialog_View.findViewById(R.id.tv_number0);
        iv_delete = (ImageView) my_dialog_View.findViewById(R.id.iv_delete);
        tv_title = (TextView) my_dialog_View.findViewById(R.id.tv_title);

        ll_dismiss = (LinearLayout) my_dialog_View.findViewById(R.id.ll_dismiss);
        setListener();

        my_dialog = new Dialog(this, R.style.dialog_normal);
        my_dialog.setContentView(my_dialog_View);

        //判断密码状态
        user = WLogin.user();
        if (TextUtils.isEmpty(user.pay_password)){
            label_code.setText(R.string.user_setpassword);
            hasPassword = false;
            tv_title.setText(R.string.user_setpassword2);
        } else{
            label_code.setText(R.string.user_changepassword);
            hasPassword = true;
            tv_title.setText(R.string.user_originalpassword);
        }
        firstInput = true;

    }

    @Override
    protected void onResume() {
        super.onResume();
        User user = WLogin.user();
        tv_money.setText(user.money);
        tv_rise.setText(user.all_earning);
    }

    /**
     * 动态适配头部背景图片高度
     */
    private void fixMyWalletBGHeight(){
        BitmapFactory.Options options = new BitmapFactory.Options();
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mywallet_bg, options);
        //获取bitmap的宽度
        float bitWidth = bitmap.getWidth();
        //获取bitmap的宽度
        float bitheight = bitmap.getHeight();
        //计算出图片的高宽比，然后按照图片的比列去缩放图片
        float bitScale =  bitheight / bitWidth;
        //获取屏幕宽度
        WindowManager wm = this.getWindowManager();
        int screenWidth = wm.getDefaultDisplay().getWidth();

        ViewGroup.LayoutParams  lp = iv_mywallet_bg.getLayoutParams();
        //设置imageView宽
        lp.height =  (int)(screenWidth * bitScale);
        iv_mywallet_bg.setLayoutParams(lp);

    }

    private void setListener(){
        ll_dismiss.setOnClickListener(listener);
        tv_number1.setOnClickListener(listener);
        tv_number2.setOnClickListener(listener);
        tv_number3.setOnClickListener(listener);
        tv_number4.setOnClickListener(listener);
        tv_number5.setOnClickListener(listener);
        tv_number6.setOnClickListener(listener);
        tv_number7.setOnClickListener(listener);
        tv_number8.setOnClickListener(listener);
        tv_number9.setOnClickListener(listener);
        tv_number0.setOnClickListener(listener);
        iv_delete.setOnClickListener(listener);
    }

    public View.OnClickListener listener = new View.OnClickListener(){

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.ll_dismiss:
                    dialogdismiss();
                    break;
                case R.id.tv_number1:
                    setNumber("1");
                    break;
                case R.id.tv_number2:
                    setNumber("2");
                    break;
                case R.id.tv_number3:
                    setNumber("3");
                    break;
                case R.id.tv_number4:
                    setNumber("4");
                    break;
                case R.id.tv_number5:
                    setNumber("5");
                    break;
                case R.id.tv_number6:
                    setNumber("6");
                    break;
                case R.id.tv_number7:
                    setNumber("7");
                    break;
                case R.id.tv_number8:
                    setNumber("8");
                    break;
                case R.id.tv_number9:
                    setNumber("9");
                    break;
                case R.id.tv_number0:
                    setNumber("0");
                    break;
                case R.id.iv_delete:
                    if(!tv_pw6.getText().toString().equals("")){
                        tv_pw6.setText("");
                    }else if(!tv_pw5.getText().toString().equals("")){
                        tv_pw5.setText("");
                    }else if(!tv_pw4.getText().toString().equals("")){
                        tv_pw4.setText("");
                    }else if(!tv_pw3.getText().toString().equals("")){
                        tv_pw3.setText("");
                    }else if(!tv_pw2.getText().toString().equals("")){
                        tv_pw2.setText("");
                    }else if(!tv_pw1.getText().toString().equals("")){
                        tv_pw1.setText("");
                    }
                    break;
            }
        }
    };

    private void setNumberNull(){
        tv_pw1.setText("");
        tv_pw2.setText("");
        tv_pw3.setText("");
        tv_pw4.setText("");
        tv_pw5.setText("");
        tv_pw6.setText("");
    }

    private void dialogdismiss(){
        my_dialog.dismiss();
        setNumberNull();
        firstInput = true;
        tmpPassWord = "";
        if(hasPassword){
            label_code.setText(R.string.user_changepassword);
            tv_title.setText(R.string.user_originalpassword);
        }else{
            label_code.setText(R.string.user_setpassword);
            tv_title.setText(R.string.user_setpassword2);
        }
    }

    private void setNumber(String num){
        String i = tv_pw1.getText().toString();
        if(tv_pw1.getText().toString().equals("")){
            tv_pw1.setText(num);
        }else if(tv_pw2.getText().toString().equals("")){
            tv_pw2.setText(num);
        }else if(tv_pw3.getText().toString().equals("")){
            tv_pw3.setText(num);
        }else if(tv_pw4.getText().toString().equals("")){
            tv_pw4.setText(num);
        }else if(tv_pw5.getText().toString().equals("")){
            tv_pw5.setText(num);
        }else if(tv_pw6.getText().toString().equals("")){
            tv_pw6.setText(num);
            String input = tv_pw1.getText().toString()
                    +tv_pw2.getText().toString()
                    +tv_pw3.getText().toString()
                    +tv_pw4.getText().toString()
                    +tv_pw5.getText().toString()
                    +tv_pw6.getText().toString();

            if(hasPassword){
                if(firstInput){
                    verifyPayPassword(input);

                }else{
                    editPayPassword(input);
                }
            }else{
                if(firstInput){
                    tmpPassWord = input;
                    firstInput = false;
                    setNumberNull();
                    tv_title.setText(R.string.user_again);
                }else{
                    if(tmpPassWord.equals(input)){
                        editPayPassword(input);

                    }else{
                        toast(getString(R.string.user_passwordinconsistency));
                        setNumberNull();
                    }
                }
            }

        }
    }

    /**
     * 验证支付密码
     */
    private void verifyPayPassword(final String password){
        loading();
        Map<String,String> params = new HashMap<>();
        params.put("password",password);
        post(EApi.USER_VERIFYPAYPASSWORD, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                firstInput = false;
                setNumberNull();
                tv_title.setText(R.string.user_newpassword);
                dismiss();
            }

            @Override
            public void failure(String msg) {
                setNumberNull();
                dismiss(msg);
            }
        });
    }

    /**
     * 修改支付密码
     */
    private void editPayPassword(final String password){
        loading();
        Map<String,String> params = new HashMap<>();
        params.put("password",password);
        post(EApi.USER_EDITPAYPASSWORD, params, new EObserver() {
            @Override
            public void successful(EResponse response) {
                toast(getString(R.string.user_passwordsuccessful));
                hasPassword = true;
                dismiss();
                dialogdismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    /**
     * 获取个人信息
     */
    private void getUserInfo(){
        loading();
        EClient.with().post(EApi.USER_INFO, new EObserver() {
            @Override
            public void successful(EResponse response) {
                User user = response.object("list",User.class);
                //保存USER
                ESPUtils.setString(MyWalletActivity.this,USER,user.toString());

                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }

        });
    }


    @OnClick(R.id.iv_leftarrow)
    protected void onPortraitClick(){
        finish();
    }

    @OnClick(R.id.rl_recharge)
    protected void onRechargeClick(){
        toActivity(MyWalletRechangeActivity.class);
    }

    @OnClick(R.id.rl_rise)
    protected void onRechargeAmountClick(){
        toActivity(RechargeAmountActivity.class);
    }

    @OnClick(R.id.rl_detail)
    protected void onWalletDetailClick(){
        toActivity(WalletDetailActivity.class);
    }

    @OnClick(R.id.rl_code)
    protected void onCodeClick(){
        my_dialog.show();
    }
}
